//
// Decompiled by Jadx - 519ms
//
package a;

import a.d;
import a.f;
import a.i;
import android.database.Cursor;
import androidx.activity.b;
import g.d;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import x.a;
import x.c;

public final class g<T> {
    public final h<T> a;
    public final ArrayList b = new ArrayList();
    public final ArrayList c = new ArrayList();
    public final a<T, ?> d;
    public Integer e;

    public g(a<T, ?> aVar) {
        this.d = aVar;
        this.a = new h<>(aVar);
    }

    public final void f(StringBuilder sb) {
        ArrayList arrayList = this.b;
        arrayList.clear();
        ArrayList arrayList2 = this.c;
        Iterator it = arrayList2.iterator();
        if (!it.hasNext()) {
            h<T> hVar = this.a;
            if (!hVar.b.isEmpty()) {
                sb.append(" WHERE ");
                ListIterator listIterator = hVar.b.listIterator();
                while (listIterator.hasNext()) {
                    if (listIterator.hasPrevious()) {
                        sb.append(" AND ");
                    }
                    i iVar = (i) listIterator.next();
                    iVar.b(sb);
                    iVar.a(arrayList);
                }
            }
            Iterator it2 = arrayList2.iterator();
            if (!it2.hasNext()) {
                return;
            }
            ((e) it2.next()).getClass();
            throw null;
        }
        e eVar = (e) it.next();
        sb.append(" JOIN ");
        sb.append('\"');
        eVar.getClass();
        throw null;
    }

    public final f<T> g() {
        a<T, ?> aVar = this.d;
        g.a aVar2 = aVar.g;
        String str = aVar2.b;
        int i = d.a;
        StringBuilder sb = new StringBuilder("SELECT ");
        String[] strArr = aVar2.d;
        int length = strArr.length;
        for (int i2 = 0; i2 < length; i2++) {
            String str2 = strArr[i2];
            sb.append("T.\"");
            sb.append(str2);
            sb.append('\"');
            if (i2 < length - 1) {
                sb.append(',');
            }
        }
        sb.append(" FROM \"");
        sb.append(str);
        sb.append("\" T ");
        StringBuilder sb2 = new StringBuilder(sb.toString());
        f(sb2);
        Integer num = this.e;
        ArrayList arrayList = this.b;
        int i3 = -1;
        if (num != null) {
            sb2.append(" LIMIT ?");
            arrayList.add(this.e);
            i3 = (-1) + arrayList.size();
        }
        String sb3 = sb2.toString();
        Object[] array = arrayList.toArray();
        int length2 = array.length;
        String[] strArr2 = new String[length2];
        for (int i4 = 0; i4 < length2; i4++) {
            Object obj = array[i4];
            if (obj != null) {
                strArr2[i4] = obj.toString();
            } else {
                strArr2[i4] = null;
            }
        }
        return new f.a(aVar, sb3, strArr2, i3).f();
    }

    public final long h() {
        a<T, ?> aVar = this.d;
        String str = aVar.g.b;
        int i = d.a;
        StringBuilder sb = new StringBuilder(b.i("SELECT COUNT(*) FROM \"", str, "\" T "));
        f(sb);
        String sb2 = sb.toString();
        Object[] array = this.b.toArray();
        int length = array.length;
        String[] strArr = new String[length];
        for (int i2 = 0; i2 < length; i2++) {
            Object obj = array[i2];
            if (obj != null) {
                strArr[i2] = obj.toString();
            } else {
                strArr[i2] = null;
            }
        }
        d f = new d.a(aVar, sb2, strArr).f();
        f.f();
        Cursor e = ((a) f).a.h.e(((a) f).d, ((a) f).c);
        try {
            if (e.moveToNext()) {
                if (e.isLast()) {
                    if (e.getColumnCount() == 1) {
                        return e.getLong(0);
                    }
                    throw new c("Unexpected column count: " + e.getColumnCount());
                }
                throw new c("Unexpected row count: " + e.getCount());
            }
            throw new c("No result for count");
        } finally {
            e.close();
        }
    }

    public final void i(int i) {
        this.e = Integer.valueOf(i);
    }

    public final ArrayList j() {
        f<T> g = g();
        g.f();
        Cursor e = ((a) g).a.h.e(((a) g).d, ((a) g).c);
        a aVar = (a) ((a) g).b.b;
        aVar.getClass();
        try {
            return aVar.w(e);
        } finally {
            e.close();
        }
    }

    public final T k() {
        T t;
        f<T> g = g();
        g.f();
        Cursor e = ((a) g).a.h.e(((a) g).d, ((a) g).c);
        a aVar = (a) ((a) g).b.b;
        aVar.getClass();
        try {
            if (!e.moveToFirst()) {
                t = null;
            } else if (e.isLast()) {
                t = (T) aVar.y(e, true);
            } else {
                throw new c("Expected unique result, but count was " + e.getCount());
            }
            return t;
        } finally {
            e.close();
        }
    }

    public final void l(i.b bVar, i... iVarArr) {
        h<T> hVar = this.a;
        hVar.c(bVar);
        ArrayList arrayList = hVar.b;
        arrayList.add(bVar);
        for (i iVar : iVarArr) {
            hVar.c(iVar);
            arrayList.add(iVar);
        }
    }
}
